package cn.csdj.sism.web;

import cn.csdj.sism.pojo.Accommodation;
import cn.csdj.sism.service.AccommodationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 住宿控制器
 * 汤思威
 */
@CrossOrigin
@RestController
@RequestMapping("/accommodation")
public class AccommodationController {
    @Autowired
    private AccommodationService accommodationService;
    @PostMapping
    public int insert(@RequestBody Accommodation accommodation){
        //如果为空就是添加
        if (StringUtils.isEmpty(accommodation.getAccommodationId())){
          return accommodationService.insert(accommodation);
        }else {
//            如果有值就是修改
          return accommodationService.update(accommodation);
        }
    }
    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Integer id){
        return accommodationService.deleteById(id);
    }
    @PutMapping
    public int update(@RequestBody Accommodation accommodation){
       return accommodationService.update(accommodation);
    }
    @GetMapping("/{id}")
    public Accommodation selectById(@PathVariable Integer id){
        return accommodationService.selectById(id);
    }
    @GetMapping
    public List<Accommodation> selectAll(){
        return accommodationService.selectAll();
    }
    @GetMapping("/page/{current}/{pageSize}")
    public IPage<Accommodation> selectPage(@PathVariable Integer current,@PathVariable Integer pageSize){
        return accommodationService.selectPages(current,pageSize);
    }
}
